package com.zijie.shoprbacapi.mapper;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.rbac.model.Perm;
import com.zijie.rbac.model.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RoleDao extends BaseMapper<Role> {
    default Page<Role> findAll(Role role, Page<Role> page) {
        LambdaQueryWrapper<Role> qw = Wrappers.lambdaQuery(role);
        return selectPage(page, qw);
    }

    @Insert("insert into t_rbac_role_perm(role_id, perm_id) values(#{adminId}, #{userGroupId})")
    boolean assignAdminToGroup(Long adminId, Long userGroupId);

    @Insert("insert into t_rbac_role_perm(role_id, perm_id) values(#{roleId}, #{permId})")
    boolean assignPermToRole(Long roleId, Long permId);

    @Select("select rp.id,rp.name,rp.description from t_rbac_role_perm rrp left join t_rbac_perm rp on rrp.perm_id=rp.id where rrp.role_id=#{roleId}")
    List<Perm> findPermsByRoleId(Long roleId);

    @Delete("delete from t_rbac_role_perm where role_id=#{roleId} and perm_id=#{permissionId}")
    boolean removeRolePermission(Long roleId, Long permissionId);
}
